//
//  SecurityCenter.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/10.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "SecurityCenter.h"
#import "HTTPGlobals.h"
@interface SecurityCenter ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIAlertViewDelegate>
{
    UITableView *_tableView;
    UIView *_headView;
}
@end

@implementation SecurityCenter

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"密保设置";
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    
    _headView = [self buildMainView];
    
}

//初始化tableview头视图
-(UIView *)buildMainView {
    UIView *headView = [[UIView alloc] init];
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 40,100 ,30)];
    textLabel.text = @"安全问答:";
    [headView addSubview:textLabel];
    NSArray *titleArr = @[@"1.母亲叫什么名字?",@"2.父亲叫什么名字?",@"3.你最喜欢的运动项目是什么?"];
    for (int i = 0; i < titleArr.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + i*50, 250, 30)];
        label.text = titleArr[i];
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame), 200, 20)];
        textField.tag = 1000+i;
        textField.delegate = self;
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(textField.frame), CGRectGetMaxY(textField.frame), 200, 1)];
        line.backgroundColor = [UIColor blackColor];
        [headView addSubview:label];
        [headView addSubview:textField];
        [headView addSubview:line];
    }
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(CGRectGetMinX(textLabel.frame), CGRectGetMaxY(textLabel.frame) + 5*30 + 30, CGRectGetWidth(self.view.frame) - 2*CGRectGetMinX(textLabel.frame), 40);
    [sureBtn setTitle:@"确定" forState:0];
    [sureBtn setTitleColor:[UIColor blackColor] forState:0];
    [sureBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [sureBtn addTarget:self action:@selector(SureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:sureBtn];
    
    headView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 300);
    return headView;
}
//确认按钮
-(void)SureBtnAction:(UIButton *)sender {
    UITextField *TF_1 = (UITextField *)[self.view viewWithTag:1000];
    UITextField *TF_2 = (UITextField *)[self.view viewWithTag:1001];
    UITextField *TF_3 = (UITextField *)[self.view viewWithTag:1002];
    if (TF_1.text.length <= 0 || TF_2.text.length <= 0 || TF_3.text.length <= 0) {
        UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"注意" message:@"回答不能为空" delegate:nil cancelButtonTitle:@"退出" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        NSArray *titleArr = @[@"1.母亲叫什么名字?",@"2.父亲叫什么名字?",@"3.你最喜欢的运动项目是什么?"];
        NSString *path = @"MobilePort/Safe/add_user_public_safe.html";
        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:titleArr[0],@"question",TF_1.text,@"answer", nil];
        NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:titleArr[1],@"question",TF_2.text,@"answer", nil];
        NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:titleArr[2],@"question",TF_3.text,@"answer", nil];
        NSArray *arr = [NSArray arrayWithObjects:dic1,dic2,dic3, nil];
        [self.HUD show:YES];
        __block int code = 0;
        //循环设置密保问题
        for (int i = 0; i<arr.count; i++) {
            //验证用户问题
            [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:path parameters:arr[i] finish:^(BOOL seccess, NSString *error, id obj) {
                if (seccess) {
                    DLog(@"用户密保设置 = %@",obj);
                    DLog(@"msg = %@",obj[@"msg"]);
                    if ([obj[@"code"] intValue] == 0) {
                        code ++;
                    }else {
                        code = 0;
                    }
                }
                if (i == arr.count - 1) {
                    [self.HUD hide:YES];
                    if (code < 3) {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"设置失败!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alert show];
                    }else {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"设置成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alert show];
                    }
                }
            }];
        }
    }
}

#pragma mark-
#pragma mark--tableviewDelegate--
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = _headView;
    return headView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    UIView *headView = [self tableView:tableView viewForHeaderInSection:section];
    
    return CGRectGetHeight(headView.frame);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    UITextField *TF_1 = (UITextField *)[self.view viewWithTag:1000];
    UITextField *TF_2 = (UITextField *)[self.view viewWithTag:1001];
    UITextField *TF_3 = (UITextField *)[self.view viewWithTag:1002];
    
    [TF_1 resignFirstResponder];
    [TF_2 resignFirstResponder];
    [TF_3 resignFirstResponder];
}


#pragma mark -
#pragma mark --textFieldDelegate--
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
#pragma mark -
#pragma mark --AlertViewDelegate--
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
